package cn.zdsoft.configuration.core.web;

import cn.zdsoft.configuration.core.Constants;
import cn.zdsoft.configuration.core.exceptions.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.util.Properties;

/**
 * Created by wangdongdong on 2018/5/14.
 */
public class WebPropertyPlaceholderConfigurer extends AbstractPropertyPlaceholderConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(WebPropertyPlaceholderConfigurer.class);

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        Properties remoteConfig = null;
        try {
            remoteConfig = (Properties) getConfigContext().getAttribute(Constants.CONFIG_CONTEXT_PROPERTIES);
            if (remoteConfig == null) {
                remoteConfig = getConfigContext().getConfigClient().getConfig();
            }
            // 本地配置文件覆盖zookeeper配置
            remoteConfig.putAll(props);
        } catch (ConfigurationException e) {
            logger.error("error in load remote config");
        }
        super.processProperties(beanFactoryToProcess, remoteConfig);
    }

}
